/**
 * Javascript library for irods audit
 *
 * Authors: Francesco Locunto - NICE (www.nice-software.com)
 *          Mike Conway - RENCI (www.renci.org)
 * Date: 07/26/2009
 */

function getCollectionAuditTrails(role, collName, startDate, endDate) {
 
	if (collName == "") {
		throw "No collection name for getCollectionAuditTrails";
	}
	
	collName.replace(/^\s+|\s+$/g,"");
  
	$("#auditResult").html("<img src='images/loading.gif'>&nbsp;&nbsp;Fetching data from iRODS, please wait...");  

	$.post("irods.xml", {_uri: "//com.enginframe.irods.audit/get_irods_audit_by_collection", _irods_collection_name: collName, _start_date: startDate, _end_date: endDate}, function(data) {
		
		// FIXME: error handling? We need it? And how?
		var errDat = checkAppError(data);
		if (errDat.status == "E") {
			//console.trace();
			//console.log("error in getCollectionAuditTrails:" + errDat.errorMinor);
			throw errDat.errorMinor;
		} 

    var auditData = data.substring((data.indexOf("##START##") + 9), data.indexOf("##END##"));
    
    var items = auditData.split("\n");
    var infos = items[0].split("|");
    var output = "<div>Audit data for collection <b>" + infos[0] + "</b> from <b>" + infos[1] + "</b> to <b>" + infos[2] + "</b>:</div><p/>";
    
    output += "<div><table id='auditData' class='list-table' cellpadding='5' cellspacing='0'><thead>";
    output += "<tr class='lt-header'>";
    output += "<th style='display: none' />";
    output += "<th style='display: none' />";
    output += "<th style='display: none' />";
    output += "<th class='lt-header-field'>Action</th>";
    output += "<th class='lt-header-field'>Comment</th>";
    output += "<th class='lt-header-field'>Date</th></tr></thead><tbody>";

    for (var i = 1; i < (items.length); i++) {
      var tokens = items[i].split("|");
      
      if (tokens.length > 1) {
        output += "<tr id='ad-row-" + i + "' class='lt-row' title='Click to show/hide metadata and ACL' style='cursor: pointer' onclick='toggleMetadataRow(" + i + ")'>";
        output += "<td id='ad-id-" + i + "' style='display: none'>" + tokens[0] + "</td>";
        output += "<td id='ad-type-" + i + "' style='display: none'>Collection</td>";
        output += "<td id='ad-object-" + i + "' style='display: none'>" + infos[0] + "</td>";
        output += "<td>" + actions[tokens[1]] + "</td>";
        output += "<td><pre class='normal-font'>" + tokens[2] + "</pre></td>";
        output += "<td>" + tokens[3] + "</td></tr>";
      }
    }

    output += "</tbody></table></div>";
    $("#auditResult").html(output);
    $("#auditData").dataTable({
      "bAutoWidth": false,
      "bSortClasses": false,
      "aoColumns": [
          {"bSearchable": false, "bSortable": false},
          {"bSearchable": false, "bSortable": false},
          {"bSearchable": false, "bSortable": false},
          null,
          null,
          null
      ],
      oLanguage: { "sSearch": "Filter:" }
    });
  }); 
}

function getFileAuditTrails(role, filename, startDate, endDate) {
	 
	if (filename == "") {
		throw "No file name for getFileAuditTrails";
	}
	
	filename.replace(/^\s+|\s+$/g,"");
	
	$("#auditResult").html("<img src='images/loading.gif'>&nbsp;&nbsp;Fetching data from iRODS, please wait...");  

	$.post("irods.xml", {_uri: "//com.enginframe.irods.audit/get_irods_audit_by_filename", _irods_file_name: filename, _start_date: startDate, _end_date: endDate, _role: role}, function(data) {
		
		// FIXME: error handling? We need it? And how?
		var errDat = checkAppError(data);
		if (errDat.status == "E") {
			//console.trace();
			//console.log("error in getCollectionAuditTrails:" + errDat.errorMinor);
			throw errDat.errorMinor;
		} 

    var auditData = data.substring((data.indexOf("##START##") + 9), data.indexOf("##END##"));
    
    var items = auditData.split("\n");
    var infos = items[0].split("|");
    var output = "<div>Audit data for file <b>" + infos[0] + "</b> from <b>" + infos[1] + "</b> to <b>" + infos[2] + "</b>:</div><p/>";
    
    output += "<div><table id='auditData' class='list-table' cellpadding='5' cellspacing='0'><thead>";
    output += "<tr class='lt-header'>";
    output += "<th style='display: none' />";
    output += "<th class='lt-header-field'>Name</th>";
    output += "<th class='lt-header-field'>Action</th>";
    output += "<th class='lt-header-field'>Comment</th>";
    output += "<th class='lt-header-field'>Date</th></tr></thead><tbody>";

    for (var i = 1; i < (items.length); i++) {
      var tokens = items[i].split("|");
      
      if (tokens.length > 1) {
        output += "<tr id='ad-row-" + i + "' class='lt-row' title='Click to show/hide metadata and ACL' style='cursor: pointer' onclick='toggleMetadataRow(" + i + ")'>";
        output += "<td id='ad-id-" + i + "' style='display: none'>" + tokens[3] + "</td>";
        output += "<td><pre id='ad-object-" + i + "' class='normal-font'>" + tokens[4] + "</td>";
        output += "<td>" + actions[tokens[0]] + "</td>";
        output += "<td><pre class='normal-font'>" + tokens[1] + "</pre></td>";
        output += "<td>" + tokens[2] + "</td></tr>";
      }
    }

    output += "</tbody></table></div>";
    $("#auditResult").html(output);
    $("#auditData").dataTable({
      "bAutoWidth": false,
      "bSortClasses": false,
      "aoColumns": [
          {"bSearchable": false, "bSortable": false},
          null,
          null,
          null,
          null
      ],
      oLanguage: { "sSearch": "Filter:" }
    });
  }); 
}
